package doantotnghiep.daoimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import doantotnghiep.dao.MeasureUnitDao;
import doantotnghiep.model.MeasureUnit;

public class MeasureUnitDaoImpl implements MeasureUnitDao {
	@Autowired
	private HibernateTemplate hibernateTemplate;
	
	@SuppressWarnings("unchecked")
	public List<MeasureUnit> getlistMeasureUnit() {
		return hibernateTemplate.find("from MeasureUnit where status = 0 order by id");
	}

	public void addMeasure(MeasureUnit measureUnit) {
		this.hibernateTemplate.saveOrUpdate(measureUnit);	
	}

	@SuppressWarnings("unchecked")
	public MeasureUnit getMeasureById(int id) {
		List<MeasureUnit> measureUnits = hibernateTemplate.find("from MeasureUnit where status = 0 and id ="+ id);
		if(measureUnits!=null){
			return measureUnits.get(0);
		}
		return null;
	}

	public void saveMeaSure(MeasureUnit measureUnit) {
		this.hibernateTemplate.saveOrUpdate(measureUnit);
	}

	public void deleteMeasure(int id) {
		MeasureUnit entity = this.getMeasureById(id);
		if(entity!=null){
			entity.setStatus(1);
			this.hibernateTemplate.saveOrUpdate(entity);
		}
	}


}
